package common.concurrency.baeldung.wait_notify;

import lombok.Data;

import java.util.concurrent.ThreadLocalRandom;

/**
 * @author zhang hao dong
 * @date 2025-02-27 14:53
 */
@Data
public class Receiver implements Runnable {
    private common.concurrency.baeldung.wait_notify.Data load;

    public Receiver(common.concurrency.baeldung.wait_notify.Data load) {
        this.load = load;
    }

    @Override
    public void run() {
        System.out.println("Receiver is running");
        for (String receivedMessage = load.receive(); !"End".equals(receivedMessage); receivedMessage = load.receive()) {
            System.out.println("Receiver:"+receivedMessage);
        }
        //Thread.sleep() to mimic heavy server-side processing
        try {
            Thread.sleep(ThreadLocalRandom.current().nextInt(5000, 7000));
            System.out.println("Receiver is sleep");
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            System.err.println("Thread Interrupted");
        }
    }
}
